﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FSY.API.Utilities;
using FSY.Components.Entities;
using FSY.API.Mail;
using FSY.Globals;
namespace FSY.Web.Admin.Users
{
    public partial class Registry1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //using Javascript for Validate user input data
            if (Session["register"] != null)
                Session.Remove("register");
            try
            {
                Fsy_User user = new Fsy_User();
                user.FirstName = txtFirstName.Text.Trim();
                user.LastName = txtLastName.Text.Trim();
                user.UserName = txtUserName.Text.Trim();
                if (txtPassword.Text == txtRetypePassword.Text)
                {
                    user.Password = txtPassword.Text;
                }
                user.Email = txtEmail.Text;
                int kq = Fsy_UserController.Insert(user);
                if (kq != 0)
                {
                    Fsy_MailInfo mail = new Fsy_MailInfo();
                    mail.MailTo = user.Email;
                    mail.MailFrom = Fsy_Config.GetAppSetting("mailFrom");
                    mail.HostMail = Fsy_Config.GetAppSetting("hostMail");
                    mail.Port = Fsy_Null.SetNullInteger(Fsy_Config.GetAppSetting("portMail"));
                    mail.UserName = Fsy_Config.GetAppSetting("userMail");
                    mail.Password = Fsy_Config.GetAppSetting("passMail");

                    if (kq != 0)
                    {
                        mail.BodyMail = string.Format(@"Chào mừng bạn đã tham gia hệ thống.....!<br />Thông tin mật khẩu đăng nhập hệ thống của bạn như dưới đây:<br />UserName:{0} <br /> Password: {1}", user.UserName, user.Password);
                        lblMessage.Text = Fsy_MailController.SendMail(mail);
                        lblMessage.Text += lblMessage.Text + "<a href=\"../Default.aspx\">Di toi trang chu</a>";
                        lblMessage.Visible = true;
                        pnlRegistry.Visible = false;
                    }
                }
            }
            catch(Exception ex)
            {
                FsyLoggerHelper.WriteError("btnSubmit_Click --" + ex.Message);
            }
        }
    }
}